package com.ql.mangersystem.service.impl;

import com.ql.mangersystem.common.Page;
import com.ql.mangersystem.entity.dto.FilePageDTO;
import com.ql.mangersystem.entity.dto.FileSearchDTO;
import com.ql.mangersystem.entity.dto.RenameDTO;
import com.ql.mangersystem.entity.po.File;
import com.ql.mangersystem.entity.vo.DownloadVo;
import com.ql.mangersystem.entity.vo.FileVo;
import com.ql.mangersystem.entity.vo.RecycleVo;
import com.ql.mangersystem.mapper.FileMapper;
import com.ql.mangersystem.service.FileService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Component
public class FileServiceImpl implements FileService {
    @Resource
    private FileMapper fileMapper;
    @Override
    public int insert(String originalFilename, String newName,String url,Integer userId) {
        LocalDateTime now = LocalDateTime.now();
        return fileMapper.insert(originalFilename,newName,now,url,userId);
    }

    @Override
    public Integer selectByNewName(String newName) {
        return fileMapper.selectByNewName(newName);
    }

    @Override
    public DownloadVo selectById(Integer id) {
        File file = fileMapper.selectById(id);
        DownloadVo downloadVo = new DownloadVo();
//        downloadVo.setFileName(file.getOriginalName());
        downloadVo.setOriginalName(file.getOriginalName());
        downloadVo.setNowName(file.getNowName());
        downloadVo.setFilePath(file.getUrl());
//        System.out.println(file);
        return downloadVo;
    }

    @Override
    public Page<FileVo> selectByPage(FilePageDTO filePageDTO) {
        Integer pageNum = filePageDTO.getPageNum();
        Integer pageSize = filePageDTO.getPageSize();
        Integer userId = filePageDTO.getUserId();
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<FileVo> page = new Page<>();
        List<FileVo> list = fileMapper.selectByPage(skipNum,pageSize,userId);
        Integer total = fileMapper.selectCount(userId);
        page.setTotal(total);
        page.setList(list);
        return page;
    }


    @Override
    public int logicDelete(Integer id) {
        LocalDateTime now = LocalDateTime.now();
        return fileMapper.logicDelete(id,now);
    }

    @Override
    public int rename(RenameDTO renameDTO) {
        Integer id = renameDTO.getId();
        String name = renameDTO.getName();
        LocalDateTime now = LocalDateTime.now();
        return fileMapper.rename(id,name,now);
    }

    @Override
    public Page<FileVo> selectByName(FileSearchDTO fileSearchDTO) {
        String fileName = fileSearchDTO.getFileName();
        Integer userId = fileSearchDTO.getUserId();
        Integer pageNum = fileSearchDTO.getPageNum();
        Integer pageSize = fileSearchDTO.getPageSize();
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<FileVo> page = new Page<>();
        List<FileVo> list = fileMapper.selectByName(skipNum,pageSize,userId,fileName);
        Integer total = fileMapper.CountByName(userId,fileName);
        page.setTotal(total);
        page.setList(list);
        return page;
    }

    @Override
    public int logicDelBatch(List<Integer> ids) {
//        List<Integer> ids =  fileDeleteBatchDTO.getIds();
        LocalDateTime time = LocalDateTime.now();
        return fileMapper.logicDelBatch(ids,time);
    }

    @Override
    public Page<RecycleVo> recycleByPage(FilePageDTO filePageDTO) {
        Integer pageNum = filePageDTO.getPageNum();
        Integer pageSize = filePageDTO.getPageSize();
        Integer userId = filePageDTO.getUserId();
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<RecycleVo> page = new Page<>();
        List<RecycleVo> list = fileMapper.recycleByPage(skipNum,pageSize,userId);
        Integer total = fileMapper.recycleCount(userId);
        page.setTotal(total);
        page.setList(list);
        return page;
    }

    @Override
    public int delete(Integer id) {
        return fileMapper.delete(id);
    }

    @Override
    public String selectName(Integer id) {
        File file = fileMapper.selectByDeleteId(id);
        return file.getNowName();
    }

    @Override
    public int recover(Integer id) {
        LocalDateTime now = LocalDateTime.now();
        return fileMapper.recover(id,now);
    }

    @Override
    public Page<RecycleVo> recycleByName(FileSearchDTO fileSearchDTO) {
        String fileName = fileSearchDTO.getFileName();
        Integer userId = fileSearchDTO.getUserId();
        Integer pageNum = fileSearchDTO.getPageNum();
        Integer pageSize = fileSearchDTO.getPageSize();
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<RecycleVo> page = new Page<>();
        List<RecycleVo> list = fileMapper.recycleByName(skipNum,pageSize,userId,fileName);
        Integer total = fileMapper.CountByName(userId,fileName);
        page.setTotal(total);
        page.setList(list);
        return page;
    }

    @Override
    public int recoverAll(List<Integer> ids) {
        LocalDateTime now = LocalDateTime.now();
        return fileMapper.recoverAll(ids,now);
    }

    @Override
    public int deleteAll(List<Integer> ids) {
        return fileMapper.deleteAll(ids);
    }
}
